<?php 

function messages_send_from_operator($message, $operator, $visitorSessionId = '') {
  $message = array(
    'visitorId'       => $visitorSessionId,
    'operatorId'      => $operator['id'],
    'administratorId' => $operator['administratorId'],
    'message'         => $message,
    'timestamp'       => gmmktime(),
    'senderName'      => $operator['name'],
  );
  database_insert('messages', $message);
  $query = "UPDATE `visitors` SET incomingUnread = 1 WHERE sessionId = '$visitorSessionId'";
  database_query($query);
}

function messages_send_from_visitor($message, $visitor) {
  $message = array(
    'visitorId'       => $visitor['sessionId'],
    'operatorId'      => $visitor['administratorId'],
    'administratorId' => $visitor['administratorId'],
    'message'         => $message,
    'timestamp'       => gmmktime(),
    'senderName'      => 'Visitor',
  );
  database_insert('messages', $message);
  $query = "UPDATE `visitors` SET outgoingUnread = 1 WHERE sessionId = '{$visitor['sessionId']}'";
  database_query($query);
  
}

function messages_send($visitorId, $message, $operatorId = 0) {
  $visitorId  = addslashes($visitorId);
  $message    = addslashes($message);
  $timestamp  = gmmktime();
  
  $query      = "
    INSERT INTO `messages` (`visitorId`, `operatorId`, `message`, `timestamp`)
    VALUES ('$visitorId', '$operatorId', '$message', $timestamp)
  ";
  
  $result     = database_query($query);
  $query      = "UPDATE `visitors` SET `unreadMessage` = 1 WHERE `sessionId` = '$visitorId'";
  $result     = database_query($query);  
}

function messages_get_all_debug() {
  $query    = "SELECT * FROM `messages` ORDER BY `id` DESC LIMIT 6";
  $data     = database_get_assoc($query);
  return $data;
}

function messages_get($visitorId = null) {
  if (!empty($visitorId)) {
    $query    = "SELECT * FROM `messages` WHERE `visitorId` = '$visitorId' ORDER BY `id` DESC LIMIT 6";
    $data     = database_get_assoc($query);
  } else {
    $operator = operators_get_account();
    $query    = "SELECT * FROM `messages` WHERE `visitorId` = '' AND administratorId = {$operator['administratorId']} ORDER BY `id` DESC LIMIT 6";
    $data     = database_get_assoc($query);
  }
  return $data;
}

function messages_check_new_incoming($visitorSessionId) {
  $query  = "SELECT * FROM `visitors` WHERE sessionId = '$visitorSessionId' AND `incomingUnread` = 1";
  $result = database_query($query);
  return mysql_num_rows($result) > 0;
}

function messages_check_new_outgoing($visitorSessionId) {
  $query  = "SELECT * FROM `visitors` WHERE sessionId = '$visitorSessionId' AND `outgoingUnread` = 1";
  $result = database_query($query);
  return mysql_num_rows($result) > 0;
}

function messages_mark_incoming_as_read($visitorId) {
  $query  = "UPDATE `visitors` SET `incomingUnread` = 0 WHERE `sessionId` = '$visitorId'";
  $result = database_query($query);
}

function messages_mark_outgoing_as_read($visitorId) {
  $query  = "UPDATE `visitors` SET `outgoingUnread` = 0 WHERE `sessionId` = '$visitorId'";
  $result = database_query($query);
}
